home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
msqc25t1
/
filemisc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-06
|
3KB
|
130 lines
/* Library FILEMISC.C: Miscellaneous file service function that extend QC */
#include <errno.h>
#include <dos.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "filemisc.h"
/*
_chmod(): Stretched version of Quick C chmod function
Changes a named file to any attribute except label and directory.
Returns 0 if successful, -1 if not, and set global variable
errno when unsuccessful.
*/
int _chmod (char far *path, int new_attrib)
{
union REGS inreg, outreg;
struct SREGS sreg;
int retval = 0;
inreg.h.ah = 0x43; /* Int 21h, fcn 43h */
inreg.h.al = 0x01; /* set file attrib */
inreg.x.cx = new_attrib; /* attrib to set */
inreg.x.dx = FP_OFF (path); /* point to file path
sreg.ds = FP_SEG (path);
int86x (0x21, &inreg, &outreg, &sreg); /* call dos */
switch (outreg.x.ax) {
case 1: errno = EINVAL; /* invalid function call */
retval = -1;
break;
case 2: /* file not found */
case 3: errno = ENOENT; /* bad path or filename */
retval = -1;
break;
case 5: errno = EACCES; /* cannot change attrib */
retval = -1;
break;
default: errno = 0; /* else no error */
}
return (retval);
}
/*
timestamp(): Converts the DOS file time stamp into:
1. A formatted string of 12 chars (hh:mm:ss ?m)
2. Its hour, min, and sec numric components
For any component not to be converted, pass NULL arg
*/
void timestamp (unsigned field,
char *string, unsigned *hour,
unsigned *min, unsigned *sec)
{
struct TSTAMP { /* time stamp bitfield */
unsigned sec : 5,
min : 6,
hour : 5;
};
union {
struct TSTAMP stamp;
unsigned ftime;
} time;
char ap[3];
int h, m, s;
strcpy (ap, "am");
time.ftime = field;
if (time.stamp.hour >23) /* get hour */
h = 0;
else
if (time.stamp.hour > 11) {
h = time.stamp.hour - 12;
ap[0] = 'p';
} else
h = time.stamp.hour;
m = time.stamp.min; /* get minute */
s = time.stamp.sec * 2; /* get seconds */
if (string) /* convert time to text string */
sprintf (string, "%02d:%02d:%02d %s", h, m, s, ap);
if (hour) *hour = h;
if (min) *min = m;
if (sec) *sec = s;
}
/*
datestamp(): Converts the DOS file date stamp into:
1. A formatted string of 11 chars (mm/dd/yyyy)
2. Its month, day, and year numeric components
For any component not to be converted, pass NULL arg
*/
void datestamp( unsigned field,
char *string, unsigned *year,
unsigned *month, unsigned *day)
{
struct DSTAMP { /* date stamp bitfield format */
unsigned day : 5,
month : 4,
year : 7;
};
union {
struct DSTAMP stamp;
unsigned fdate;
} date;
unsigned d, m, y;
date.fdate = field;
y = date.stamp.year + 1980;
m = date.stamp.month;
d = date.stamp.day;
if (string)
sprintf (string, "%02d/%02d/%02d", m, d, y);
if (year) *year = y;
if (month) *month = m;
if (day) *day = d;
}